package com.wk.controller.student;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wk.pojo.*;
import com.wk.service.Blogsservice;
import com.wk.utils.PaginationConstant;
import com.wk.utils.responseresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @program: thesis
 * @description: 学生的社区管理
 * @author: 王侃
 * @create: 2020-03-02 10:37
 **/
@Controller
public class Stucommunitycontroller {

    @Autowired
    Blogsservice blogsservice;

    //跳转到话题主界面,根据时间
    @RequestMapping("/community/index")
    public String com_index(Integer page,String subject, String username,Model model)
    {
        //使用分页插件
        //对第一次传值进行判断
        if (ObjectUtils.isEmpty(page)) {
            page = PaginationConstant.CURRENT_NUM;
        }
        //设置分页
        PageHelper.startPage(page, PaginationConstant.PAGE_SIZE);
        List<Blogs> blogs=blogsservice.selectbyallblogs(subject,username);
        PageInfo<Blogs> pageInfo=new PageInfo<>(blogs);

        model.addAttribute("subject",subject);
        model.addAttribute("username",username);
        model.addAttribute("pageinfo",pageInfo);

        return "student/community/index";
    }

    //跳转到话题主界面，根据点赞数
    @RequestMapping("/community/index_praise")
    public String com_index_praise(Integer page,String subject,String username,Model model)
    {
        //使用分页插件
        //对第一次传值进行判断
        if (ObjectUtils.isEmpty(page)) {
            page = PaginationConstant.CURRENT_NUM;
        }
        //设置分页
        PageHelper.startPage(page, PaginationConstant.PAGE_SIZE);
        List<Blogs> blogs=blogsservice.selectbyallblogs_praise(subject,username);
        PageInfo<Blogs> pageInfo=new PageInfo<>(blogs);
        model.addAttribute("pageinfo",pageInfo);
        model.addAttribute("subject",subject);
        model.addAttribute("username",username);
        return "student/community/index_praise";
    }


    //跳转到发布话题
    @RequestMapping("/stu/community/commenting")
    public String com_commenting()
    {
        return "student/community/commenting";
    }

    //发布话题到数据库
    @RequestMapping("/stu/community/commentsubmit")
    public String com_commentsubmit(Blogs blogs, HttpSession session)
    {
        //获得本用户的session信息
        User user=(User)session.getAttribute("loginuser");
        blogs.setPraise(0);
        blogs.setUser_id(user.getId());
        blogs.setUsername(user.getUsername());
        blogs.setCreatetime(new Timestamp(new Date().getTime()));
        //将类信息加入数据库中
        blogsservice.insertintoBlogs(blogs);
        return "redirect:/community/index";
    }

    //让话题的点赞数+1
    @RequestMapping("/stu/community/praise")
    @ResponseBody
    public responseresult com_praise(Integer id)
    {
        blogsservice.updatebyidpraise_blog(id);
        return responseresult.success();
    }

    //跳转到话题管理界面
    @RequestMapping("/stu/community/management")
    public String com_management(HttpSession session,Model model)
    {
        User user=(User)session.getAttribute("loginuser");
        List<Blogs> blogs=blogsservice.selectbyuserid_blogs(user.getId());
        model.addAttribute("blogs",blogs);
        return "student/community/management";
    }

    //修改话题前获得信息
    @RequestMapping("/stu/community/getmyindex/{id}")
    public String com_getmyindex(@PathVariable("id") Integer id,Model model)
    {
        Blogs blogs=blogsservice.selectbyidblog(id);
        model.addAttribute("blog",blogs);
        return "student/community/update";
    }

    //修改话题成功并且提交到数据库
    @RequestMapping("/stu/community/updatesubmit")
    public String com_updatesubmit(Blogs blogs)
    {
        blogs.setCreatetime(new Timestamp(new Date().getTime()));
        blogsservice.updatebyblog(blogs);

        return "redirect:/stu/community/management";
    }

    //删除id话题
    @RequestMapping("/stu/community/delete/{id}")
    public String com_delete(@PathVariable("id") Integer id)
    {
        blogsservice.deletebyidblog(id);
        return "redirect:/stu/community/management";
    }

    //跳转到该话题的详细界面
    @RequestMapping("/community/blog/detailed/{id}")
    public String blog_detailed(@PathVariable("id") Integer id,Model model)
    {
        Blogs blogs=blogsservice.selectbyidblog(id);
        model.addAttribute("blog",blogs);

        //显示出所有的评论信息
        List<Blogs_comment> comments=blogsservice.selectbyallblog_comment(id);
        model.addAttribute("comments",comments);
        return "student/community/blog_comment";
    }

    //提交评论到数据库
    @RequestMapping("/stu/community/blog/comment/submit")
    public String comment_submit(HttpSession session,Blogs_comment comment)
    {
        User user=(User)session.getAttribute("loginuser");
        comment.setCreatetime(new Timestamp(new Date().getTime()));
        comment.setPraise(0);
        comment.setUser_id(user.getId());
        comment.setUsername(user.getUsername());
//        Integer ID=Integer.parseInt(comment.getBlog_id());

        blogsservice.insertintoblog_comment(comment);
        return "redirect:/community/blog/detailed/"+comment.getBlog_id();
    }

    //删除自己的评论
    @RequestMapping("/stu/community/blog/comment/delete/{id}/{blog_id}")
    public String comment_delete(@PathVariable("id")Integer id,@PathVariable("blog_id") Integer blog_id)
    {
//        System.out.println(id);
        blogsservice.deletebyidcomment(id);
        return "redirect:/community/blog/detailed/"+blog_id;
    }

    //让评论的点赞数+1
    @RequestMapping("/stu/community/blog/comment/praise")
    @ResponseBody
    public responseresult comment_praise(Integer id)
    {
         blogsservice.updateidcomment_praise(id);
        return responseresult.success();
    }

    //跳转到评论回复界面
    @RequestMapping("/stu/community/blog/comment/reply/{id}")
    public String comment_reply(@PathVariable("id")Integer id,Model model)
    {
        Blogs_comment comment=blogsservice.selectbyid_blogs_comment(id);
        model.addAttribute("comment",comment);

        List<Blogs_reply> replies=blogsservice.selectbycomment_id_reply(id);
        model.addAttribute("replies",replies);
        return "student/community/blog_reply";
    }

    //提交评论的回复到数据库中
    @RequestMapping("/stu/community/blog/comment/reply/submit")
    public String reply_submit(Blogs_reply reply,HttpSession session)
    {
        User user=(User)session.getAttribute("loginuser");
        reply.setUser_id(user.getId());
        reply.setUsername(user.getUsername());
        reply.setCreatetime(new Timestamp(new Date().getTime()));

        blogsservice.insertintocomment_reply(reply);
        return "redirect:/stu/community/blog/comment/reply/"+reply.getComment_id();
    }

    //删除自己的回复
    @RequestMapping("/stu/community/blog/comment/reply/delete/{comment_id}/{id}")
    public String reply_delete(@PathVariable("comment_id")Integer comment_id,@PathVariable("id")Integer id)
    {
        blogsservice.deletemycomment_reply(id);
        return "redirect:/stu/community/blog/comment/reply/"+comment_id;
    }
}
